//
//  ZHLoginView.m
//  Doctor
//
//  Created by liuzhishan on 15/12/14.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "ZHLoginView.h"
#import "ZHAlertView.h"

#define WIGHTDISTANCE  20

@interface ZHLoginView ()

@property (nonatomic,retain) UIImageView *imageBg;


@property (nonatomic,retain) UIImageView *imageUserNameBg;

@property (nonatomic,retain) UIImageView *imageUserPswBg;

@property (nonatomic,retain) UITextField *filedUserName;

@property (nonatomic,retain) UITextField *filedUserPsw;



@property (nonatomic,retain) UIButton *btnChoose;

@property (nonatomic,assign) BOOL isChooseing;

@property (nonatomic,strong) ZHAlertView *alertUp;
@property (nonatomic,strong) ZHAlertView *alertDown;





@end

@implementation ZHLoginView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    
    if (self) {
        
        
        [self addMyChildViews];

        
    }
    
    
    return self;
}

-(void)addMyChildViews
{
    
    self.isChooseing = NO;
    
    self.imageBg = [[UIImageView alloc]init];
    [self.imageBg setMyFramewithX:0 andY:0 andWidth:435 andHeight:365];
    self.imageBg.image = [UIImage imageNamed:@"login-bg1"];
 
    [self addSubview:self.imageBg];
    
    self.imageUserNameBg = [[UIImageView alloc]init];
    self.imageUserNameBg.image = [UIImage imageNamed:@"login-fieldbg"];
    [self.imageUserNameBg setMyFramewithX:85 andY:92 andWidth:250 andHeight:43];
    [self addSubview:self.imageUserNameBg];
    
    self.filedUserName  = [[UITextField alloc]init];
//    self.filedUserName.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.filedUserName.frame = self.imageUserNameBg.frame;
    self.filedUserName.x += WIGHTDISTANCE*WIDPro;
    self.filedUserName.width -= 2*WIGHTDISTANCE*WIDPro;
    self.filedUserName.placeholder = @"请输入用户名：";
    self.filedUserName.font= [UIFont systemFontOfSize:17 * WIDPro - 2];
    self.filedUserName.returnKeyType = UITextAutocapitalizationTypeNone;
    [self addSubview:self.filedUserName];
    
    self.imageUserPswBg = [[UIImageView alloc]init];
    self.imageUserPswBg.image = [UIImage imageNamed:@"login-fieldbg"];
    
    [self.imageUserPswBg setMyFramewithX:85 andY:168 andWidth:250 andHeight:43];
    [self addSubview:self.imageUserPswBg];
    
    self.filedUserPsw  = [[UITextField alloc]init];
    self.filedUserPsw.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.filedUserPsw.frame = self.imageUserPswBg.frame;
    self.filedUserPsw.x += WIGHTDISTANCE*WIDPro;
    self.filedUserPsw.width -= 2*WIGHTDISTANCE*WIDPro;
    self.filedUserPsw.placeholder = @"请输入密码：";
    self.filedUserPsw.font = [UIFont systemFontOfSize:17 * WIDPro - 2];
    self.filedUserPsw.secureTextEntry = YES;
    [self addSubview:self.filedUserPsw];
    
    self.btnChoose = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnChoose setMyFramewithX:344 andY:180 andWidth:19 andHeight:17];
    self.btnChoose.centerY = self.filedUserPsw.centerY;
    [self.btnChoose setBackgroundImage:[UIImage imageNamed:@"login-NotShowPsw"] forState:(UIControlStateNormal)];
    [self.btnChoose addTarget:self action:@selector(chooseAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.btnChoose];
    
    self.btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    
   [self.btnLogin setMyFramewithX:120 andY:249 andWidth:172 andHeight:35];
   [self.btnLogin setBackgroundImage:[UIImage imageNamed:@"login-loginbtn"] forState:UIControlStateNormal];
    
    
    
    [self.btnLogin addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.btnLogin];
    
    

    ZHAlertView *alertView = [[ZHAlertView alloc]init];
    [alertView setMyFramewithX:355 andY:90 andWidth:200 andHeight:40];
    [self addSubview:alertView];
    self.alertUp = alertView;
    self.alertUp.alpha = 0;
   
    
    alertView = [[ZHAlertView alloc]init];
    [alertView setMyFramewithX:355 andY:170 andWidth:200 andHeight:40];
    [alertView setAlertMessage:@"亲～～密码错误"];
    [self addSubview:alertView];
    self.alertDown = alertView;
    self.alertDown.alpha = 0;
    
    
    
    
    
    
    
}

-(void)alertUP:(CGFloat)dismissTime withMessage:(NSString *)meg
{
    [UIView animateWithDuration:0.1 animations:^{
        
        self.alertUp.alpha = 1.0;
        [self.alertUp setAlertMessage:meg];
        
    } completion:^(BOOL finished) {
        
       [UIView animateWithDuration:dismissTime * 2.0 animations:^{
           
           self.alertUp.alpha = 0;
       }];
        
    }];
        
        
        

}
-(void)alertDown:(CGFloat)dismissTime
{
    [UIView animateWithDuration:0.2 animations:^{
        
        self.alertDown.alpha = 1.0;
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:dismissTime * 2.0 animations:^{
            
            self.alertDown.alpha = 0;
        }];
        
    }];

}

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}

-(void)loginAction
{
   
//    [self.filedUserName resignFirstResponder];
//    [self.filedUserPsw resignFirstResponder];
    
    
    [self.dele loginBtnisClick:self.filedUserName.text andPsw:self.filedUserPsw.text];
    
}

-(void)chooseAction
{
    
    
    if (self.isChooseing == YES) {
        
        [self.btnChoose setBackgroundImage:[UIImage imageNamed:@"login-NotShowPsw"] forState:UIControlStateNormal];
        
        self.filedUserPsw.secureTextEntry = YES;
        
            }
    
    else
    {
        
         [self.btnChoose setBackgroundImage:[UIImage imageNamed:@"login-ShowPsw"] forState:UIControlStateNormal];
        
        self.filedUserPsw.secureTextEntry = NO;
        
    }
    
    
    self.isChooseing = !self.isChooseing;
    
    
}


-(void)saveTheUserMessageToLocal
{
    
    [[NSUserDefaults standardUserDefaults] setValue:@"yes" forKey:@"loginTrue"];
    [[NSUserDefaults standardUserDefaults] setValue:self.filedUserName.text forKey:@"userName"];
    [[NSUserDefaults standardUserDefaults] setValue:self.filedUserPsw.text forKey:@"passWord"];
}

@end
